﻿
using SqlSugar.Extensions;

namespace TestPressure
{
    public partial class EditDevicePointForm:BaseForm
    {
        #region 属性

        /// <summary>点位
        /// 
        /// </summary>
        private DevicePoint obj = new DevicePoint();

        public DevicePointItem? item;

        /// <summary>设备
        /// 
        /// </summary>
        public DeviceItem? deviceItem;

        #endregion

        #region 自定义方法

        /// <summary>重载
        /// 
        /// </summary>
        private void Reload()
        {
            PointNumberInput.Text = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
            PointNameInput.Text = "";
            PointAddressInput.Text = "";
            RangeMinInput.Text = "";
            RangeMaxInput.Text = "";
            OutMinInput.Text = "";
            OutMaxInput.Text = "";
            AmendInput.Text = "";
            AlarmHightInput.Text = "";
            AlarmLowInput.Text = "";
            ErrorRangeInput.Text = "";
            UnitInput.Text = "";
            RegisterTypeSelect.Text = "";
        }

        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["device_number"] = DeviceNumberInput.Text;
            where["point_name"] = PointNameInput.Text;
            List<DevicePoint> list = SqlSugarHelper.Inquireable<DevicePoint>(where);
            int count = list.Count;
            if (count > 0)
            {
                string msg = string.Format("【{0}】设备已存在【{1}】点位", deviceItem.deviceName, where["point_name"]);
                Error(msg);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>判断设备是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckDeviceExistWithNumber(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["device_number"] = number;
            List<Device> list = SqlSugarHelper.Inquireable<Device>(where);

            if (list.Count == 0)
            {
                string msg = String.Format("【{0}】编号设备不存在，无法添加点位", number);
                Error(msg);
                return false;
            }
            else
            {
                Device device = list[0];
                if (device.device_name is not null)
                {
                    DeviceNameInput.Text = device.device_name;
                }

                DeviceNumberInput.Text = device.device_number.ToString();
                DeviceAddressInput.Text = device.device_address.ToString();
                return true;
            }
        }

        private int RegisterTypeToInt(string typeString)
        {
            int type = 0;
            if (typeString == "DI")
            {
                type = 1;
            }
            else if (typeString == "DO")
            {
                type = 2;
            }
            else if (typeString == "Input")
            {
                type = 3;
            }
            else if (typeString == "Holding")
            {
                type = 4;
            }
            return type;
        }

        #endregion

        #region 系统方法
        public EditDevicePointForm()
        {
            InitializeComponent();

        }

        private void EditDevicePointForm_Load(object sender, EventArgs e)
        {
            this.Header.Text = "编辑设备点位";
            Size size = new Size(int.Parse((574 * this.Dpi()).ToString()), int.Parse((385 * this.Dpi()).ToString()));
            this.MinimumSize = size;
            this.MaximumSize = size;
            Reload();
            if (deviceItem is not null)
            {
                bool res = CheckDeviceExistWithNumber(deviceItem.deviceNumber.ObjToInt());
                if (!res)
                {
                    return;
                }
            }
            else
            {
                DevicePointManageForm frm = (DevicePointManageForm)this.Owner;
                Alert.Error("无法获取设备信息", frm);
                frm.LoadList();
                this.Close();
                this.Dispose();
            }

        }
        #endregion

        #region 点击事件
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            
            if (PointNameInput.Text.Length == 0)
            {
                Error("请输入点位名称");
                PointNameInput.Focus();
                return;
            }
            obj.device_name = DeviceNameInput.Text;

            if (PointAddressInput.Text.Length == 0)
            {
                Error("请输入点位地址");
                PointAddressInput.Focus();
                return;
            }
            obj.device_address = OutMaxInput.Text.ObjToInt();

            if (RegisterTypeSelect.Text.Length == 0)
            {
                Error("请选择存储类型");
                RegisterTypeSelect.Focus();
                return;
            }
            
            bool repeatRes = CheckRepeat();
            if (repeatRes)
            {
                return;
            }
            obj.device_number = DeviceNumberInput.Text.ObjToInt();
            obj.device_name = DeviceNameInput.Text;
            obj.device_address = DeviceAddressInput.Text.ObjToInt();
            obj.point_number = PointNumberInput.Text.ObjToInt();
            obj.point_name = PointNameInput.Text;
            obj.point_address = PointAddressInput.Text.ObjToInt();
            obj.range_max = RangeMaxInput.Text.ObjToInt();
            obj.range_min = RangeMinInput.Text.ObjToInt();
            obj.out_max = OutMaxInput.Text.ObjToInt();
            obj.out_min = OutMinInput.Text.ObjToInt();
            obj.amend = AmendInput.Text.ObjToInt();
            obj.alarm_hight = AlarmHightInput.Text.ObjToInt();
            obj.alarm_low = AlarmLowInput.Text.ObjToInt();
            obj.error_range = ErrorRangeInput.Text.ObjToInt();
            obj.unit = UnitInput.Text;
            obj.register_type = RegisterTypeToInt(RegisterTypeSelect.Text);
            List<DevicePoint> list = new List<DevicePoint>();
            list.Add(obj);
            int res = SqlSugarHelper.Insertable(list);
            if (res > 0)
            {
                Reload();
                if (this.Owner is not null)
                {
                    DevicePointManageForm frm = (DevicePointManageForm)Owner;
                    frm.LoadList();
                }
                this.Close();
            }
            else
            {
               Error("添加点位失败");
            }
        }
        #endregion
    }
}
